<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Goods;
use App\Models\GoodsTZL;
use App\Utils\TianZhenLan;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public function tongbu_tzl_goods() {
        TianZhenLan::get_products();
        return self::success();
    }

    public function select_tzl_goods(Request $request) {
        $id = $request->input('id');
        $tzlGoods = GoodsTZL::find($id);
        $tzlGoods->select = 1;
        $tzlGoods->save();
        Goods::create([
            'gid'   => $tzlGoods->product_code,
            'sn'    => 'TZL_'.$tzlGoods->product_code,
            'name'  => $tzlGoods->subtitle,
            'detail_img' => $tzlGoods->main_img,
            'company' => Company::TZL
        ]);

        return self::success();
    }

    public function delete(Request $request) {
        $id = $request->input('id');
        $goods = Goods::find($id);
        if ($goods->company == Company::TZL) {
            GoodsTZL::where('product_code', $goods->gid)->update(['select' => 0]);
        }
        Goods::destroy($id);
        return self::success();
    }

}
